package com.mall.biz.eshop;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import com.mall.client.eshop.EshopCompanyService;
import com.mall.client.eshop.EshopService;
import com.mall.client.eshop.EshopTrademarkService;
import com.mall.client.eshop.EshopUserInfoService;
import com.mall.client.eshop.SaleEshopService;
import com.mall.pojo.Eshop;
import com.mall.pojo.EshopCompany;
import com.mall.pojo.EshopExample;
import com.mall.pojo.EshopTrademark;
import com.mall.pojo.EshopUserInfo;
import com.mall.pojo.EshopUserInfoExample;
import com.mall.pojo.EshopUserInfoExample.Criteria;
import com.mall.pojo.MallRecommend;
import com.mall.pojo.SaleEshop;
import com.mall.pojo.User;
import com.mall.query.eshop.EshopQuery;
import com.mall.query.eshop.EshopUserQuery;
import com.mall.vo.eshop.SaleEshopVO;
import com.mall.web.util.WebUtil;
@Service
/**
 * maigedian的biz层
 * @author Administrator
 *
 */
public class EshopBizImpl {
	@Autowired
	private SaleEshopService salEshopService;
	@Autowired
	private EshopCompanyService eshopCompanyService;
	@Autowired
	private EshopTrademarkService eshopTrademarkService;
	@Autowired
	private SaleEshopService saleEshopService;
	@Autowired
	private EshopUserInfoService eshopUserInfoService;
	@Autowired
	private EshopService eshopService;
	
	/**
	 * 获得所有首页推荐的saleEshop
	 * @param mallRecommendList
	 * @return
	 */
	public List<SaleEshop> getSaleEshopListByRecommend(List<MallRecommend> mallRecommendList){
		if(mallRecommendList == null || mallRecommendList.size() <= 0){
			return null;
		}
		List<Long> ids = new ArrayList<Long>();
		for(MallRecommend mallRecommend : mallRecommendList){
			ids.add(mallRecommend.getMallId());
		}
		return this.salEshopService.getSaleEshopListByIds(ids);
	}
	
	/**
	 * 根据saleEshop获得SaleEshopVO
	 * @param saleEshopList
	 * @return
	 */
	public List<SaleEshopVO> getSaleEshopVOListBySaleEshopList(List<SaleEshop> saleEshopList){
		if(saleEshopList == null || saleEshopList.size() <= 0){
			return null;
		}
		
		List<SaleEshopVO> saleEshopVOList = new ArrayList<SaleEshopVO>();
		for(SaleEshop saleEshop : saleEshopList){
			SaleEshopVO saleEshopVO = new SaleEshopVO();
			saleEshopVO.setEshopCompany(this.eshopCompanyService.getEshopCompanyByEshopCompanyId(saleEshop.getEshopCompanyId()));
			saleEshopVO.setEshopTrademark(this.eshopTrademarkService.getEshopTrademarkByEshopTrademarkId(saleEshop.getTrademarkId()));
			saleEshopVO.setSaleEshop(saleEshop);
			saleEshopVOList.add(saleEshopVO);
		}
		return saleEshopVOList;
	}
	
	
	/**
	 * 根据Eshop获得SaleEshopVO
	 * @param saleEshopList
	 * @return
	 */
	public List<SaleEshopVO> getSaleEshopVOListByEshopList(List<Eshop> eshopList){
		if(eshopList == null || eshopList.size() <= 0){
			return null;
		}
		
		List<SaleEshopVO> saleEshopVOList = new ArrayList<SaleEshopVO>();
		for(Eshop eshop : eshopList){
			SaleEshopVO saleEshopVO = new SaleEshopVO();
			SaleEshop saleEshop = this.saleEshopService.getSaleEshopBySaleEshopId(eshop.getSaleEshopId());
			saleEshopVO.setEshopCompany(this.eshopCompanyService.getEshopCompanyByEshopCompanyId(eshop.getEshopCompanyId()));
			saleEshopVO.setEshopTrademark(this.eshopTrademarkService.getEshopTrademarkByEshopTrademarkId(eshop.getTrademarkId()));
			saleEshopVO.setSaleEshop(saleEshop);
			saleEshopVO.setEshop(eshop);
			saleEshopVO.setCategoryName(saleEshop.getCategoryName());
			saleEshopVOList.add(saleEshopVO);
		}
		return saleEshopVOList;
	}

	/**
	 * 根据saleEshopId获得saleEshopVO对象
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	public SaleEshopVO getSaleEshopBySaleEshopId(Long saleEshopId){
		if (saleEshopId == null || saleEshopId <= 0) {
			return null;
		}
		SaleEshopVO saleEshopVO = new SaleEshopVO();
		SaleEshop saleEshop = this.saleEshopService.getSaleEshopBySaleEshopId(saleEshopId);
		if(saleEshop == null){
			return null;
		}
		EshopCompany eshopCompany = null;
		if(saleEshop.getEshopCompanyId() != null && saleEshop.getEshopCompanyId() > 0){
			eshopCompany = this.eshopCompanyService.getEshopCompanyByEshopCompanyId(saleEshop.getEshopCompanyId());
		}
		EshopTrademark eshopTrademark = null;
		if(saleEshop.getTrademarkId() != null && saleEshop.getTrademarkId() > 0){
			eshopTrademark = this.eshopTrademarkService.getEshopTrademarkByEshopTrademarkId(saleEshop.getTrademarkId());
		}
		saleEshopVO.setEshopCompany(eshopCompany);
		saleEshopVO.setEshopTrademark(eshopTrademark);
		saleEshopVO.setSaleEshop(saleEshop);
		
		return saleEshopVO;
	}
	
	
	/**
	 * 将当前用户信息放入到modelMap里
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	public ModelMap putUserIntoMap(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
		User user = WebUtil.getCurrentUser(request);
		modelMap.put("user", user);
		return modelMap;
	}
	/**
	 * 快速留言列表
	 * @param eshopUserQuery
	 * @return
	 */
	public List<EshopUserInfo> getEshopUserInfoListByQuery(EshopUserQuery eshopUserQuery){
		EshopUserInfoExample example = eshopUserInfoExample(eshopUserQuery);
		List<EshopUserInfo> eshopUserInfoList = this.eshopUserInfoService.getEshopUserInfoListBySellerInfoExample(example);
		
		return eshopUserInfoList;
		
	}
	
	/**
	 * Eshop总数
	 * @param eshopQuery
	 * @return
	 */
	public int getEshopCountByQuery(EshopQuery eshopQuery){
		EshopExample eshopExample = getEshopExampleByQuery(eshopQuery);
		return this.eshopService.getEshopCount(eshopExample);
		
	}
	
	/**
	 * EshopUserInfo总数
	 * @param eshopUserQuery
	 * @return
	 */
	public int getEshopUserInfoCountByQuery(EshopUserQuery eshopUserQuery){
		EshopUserInfoExample example = eshopUserInfoExample(eshopUserQuery);
		Integer count = this.eshopUserInfoService.getEshopUserInfoCountByExample(example);
		
		return count;
		
	}
	
	
	public EshopUserInfoExample eshopUserInfoExample(EshopUserQuery eshopUserQuery){
		EshopUserInfoExample sellerInfoExample = new EshopUserInfoExample();
		if (eshopUserQuery.getOrderby()!=null) {
			sellerInfoExample.setOrderByClause(eshopUserQuery.getOrderby());
		}
		
		if(eshopUserQuery.getPage() !=null){
			sellerInfoExample.setLimit(eshopUserQuery.getPage().getLength());
			sellerInfoExample.setStart(eshopUserQuery.getPage().getBegin());
		}
		Criteria criteria = sellerInfoExample.createCriteria();
		if (eshopUserQuery.getType() != null) {
			criteria.andTypeEqualTo(eshopUserQuery.getType());
		}
		if (StringUtils.isNotBlank(eshopUserQuery.getKeyWord())) {
			criteria.andRemarkLike("%"+eshopUserQuery.getKeyWord()+"%");
		}
		if (eshopUserQuery.getOnlyKey() != null) {
			criteria.andCommOnlyKeyEqualTo(eshopUserQuery.getOnlyKey());
		}
		if (eshopUserQuery.getStatus() != null) {
			criteria.andStatusEqualTo(eshopUserQuery.getStatus());
		}
		return sellerInfoExample;
		
	}
	
	/**
	 * 根据eshopquery查询eshop列表
	 * @param id
	 * @return
	 */
	public List<Eshop> getEshopListByEshopQuery(EshopQuery eshopQuery) {
		EshopExample eshopExample = getEshopExampleByQuery(eshopQuery);
		return this.eshopService.getEshopListByEshopExample(eshopExample);
	}
	
	public EshopExample getEshopExampleByQuery(EshopQuery eshopQuery){
		EshopExample eshopExample = new EshopExample();
		if(eshopQuery.getPage() != null){
			eshopExample.setStart(eshopQuery.getPage().getBegin());
			eshopExample.setLimit(eshopQuery.getPage().getLength());
		}
		if(StringUtils.isNotBlank(eshopQuery.getOrderBy())){
			eshopExample.setOrderByClause(eshopQuery.getOrderBy());
		}
		com.mall.pojo.EshopExample.Criteria criteria = eshopExample.createCriteria();
		if(eshopQuery.getEshopSellerId() != null){
			criteria.andEshopSellerIdEqualTo(eshopQuery.getEshopSellerId());
		}
		return eshopExample;
	}
	
}
